package com.poker.controller;

import com.poker.entity.Permission;
import com.poker.entity.Role;
import com.poker.service.PermissionService;
import com.poker.service.RoleService;
import com.poker.util.Misc;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: yuanZ;
 * @Date: 2018/8/1 17:29;
 * @Description: 角色管理;
**/
@Controller
@RequestMapping(value = "/managerRole")
public class RoleController extends BaseController {

    @Resource
    private RoleService roleService;

    @Resource
    private PermissionService permissionService;

    @RequestMapping("/list")
    public String list(Model model, Role role,
                       @RequestParam(defaultValue = "0") Integer page,
                       @RequestParam(defaultValue = "10") Integer size) {
        Page<Role> data = roleService.findRoleNoCriteria(page, size);
        model.addAttribute("role", role);
        model.addAttribute("data", data);
        return managerPage("role/list");
    }

    @RequestMapping("/findRoleQuery")
    public String findRoleQuery(Model model , Role role,
                                @RequestParam(defaultValue = "0") Integer page,
                                @RequestParam(defaultValue = "10") Integer size) {
        Page<Role> data = roleService.findRoleCriteria(page, size, role);
        model.addAttribute("role", role);
        model.addAttribute("data", data);
        return managerPage("role/list");
    }

    @RequestMapping("/delete")
    public String delete(String id, Integer page) {
        roleService.deleteById(id);
        return redirect("/managerRole/list?page=" + page);
    }

    @RequestMapping("/edit")
    public String edit(Model model, String id) {
        Role role = new Role();

        if (Misc.isNotEmpty(id)) role = roleService.getById(id);

        model.addAttribute("role", role);
        return managerPage("role/edit");
    }

    @RequestMapping("/add")
    public String add(Role role, String permissions) {
        String[] array = permissions.split(",");

        Set<Permission> set = new HashSet<>();
        for (String anArray : array) { set.add(permissionService.getById(anArray)); }

        role.setPermissions(set);
        roleService.save(role);

        return redirect("/managerRole/list");
    }

    @ResponseBody
    @RequestMapping("/getRole")
    public String getPermission() {
        List<Role> data = roleService.findAll();

        for (Role role : data) {
            role.setPermissions(null);
            role.setManagers(null);
        }

        return toJson(true, "success", data);
    }

    /*Test*/
    @RequestMapping("/userList")
    //@RequiresRoles(value = {"user", "admin"}, logical = Logical.OR)
    @RequiresPermissions("managerInfo:view")//权限管理;
    public String userInfo(){
        return "manager/userInfo";
    }

}
